-module(rtps_ofo_sup).

%%% @doc This module implements a generic one-for-one supervisor which
%%% is used as the supervisor for domains, participant, writers,
%%% readers, reader locators, reader proxies and writer proxies. Each
%%% of these entities operates on its own and independent of similar
%%% entities. For example, one domain is fully independent of another
%%% domain and in case of failure should be restarted on its own.
%%%
%%% NB: Domains, participants etc, are themselves implemented by a
%%% one-for-all supervisor structure which oversees the different
%%% building blocks of these entities. These one-for-all supervisors
%%% are the direct children of this generic one-for-one supervisor.

-behaviour(supervisor).

%% API
-export([start_link/0, start_child/2, stop_child/2]).

%% Supervisor callbacks
-export([init/1]).

%%====================================================================
%% API functions
%%====================================================================

-spec start_child(Sup, Child_spec) -> {ok, Child} | {error, Reason} when
      Sup :: pid(),
      Child_spec :: supervisor:child_spec(),
      Child :: pid(),
      Reason :: term().
start_child(Sup, Child_spec) ->
    supervisor:start_child(Sup, Child_spec).

-spec stop_child(Sup, Child_id) -> Result when
      Sup :: pid(),
      Child_id :: term(),
      Result :: ok | {error, Error},
      Error :: running | restarting | not_found.
stop_child(Sup, Child_id) ->
    case supervisor:terminate_child(Sup, Child_id) of
	ok ->
	    %% TODO: remove catch. Maybe still needed in certain shutdown situations
	    catch supervisor:delete_child(Sup, Child_id);
	Error ->
	    Error
    end.
    
start_link() ->
    supervisor:start_link(?MODULE, []).

%%====================================================================
%% Supervisor callbacks
%%====================================================================

%% @private
init([]) ->
    {ok, {#{strategy => one_for_one}, []}}.

%%====================================================================
%% Internal functions
%%====================================================================

